package com.by_man.presenter.menu_activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.by_man.presenter.R;
import com.by_man.presenter.SharedApplication;
import com.by_man.presenter.main_page.MainActivity;
import com.by_man.repository.IpAddress;
import com.by_man.repository.PostJsonArrayRequest;
import com.by_man.repository.UserInfos;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by man on 2017-03-09.
 */
public class ServiceIntroductionActivity extends AppCompatActivity {
    private RequestQueue requestQueue;
    private TextView serviceView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_introduction);
        //requestQueue = Volley.newRequestQueue(this);
        requestQueue = SharedApplication.getrQueue(getApplication());
        requestQueue.start();
        //页面相关组件的初始化
        initView();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(this, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
        return super.onKeyDown(keyCode,event);
    }

    private void initView() {
        // 初始化ToolBar
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeAsUpIndicator(R.drawable.icon_back);
        actionBar.setDisplayHomeAsUpEnabled(true);

        serviceView = (TextView) findViewById(R.id.serviceContentView);
        HashMap<String, String> params = new HashMap<>();
        params.put("token", UserInfos.token);
        JSONObject jsonObject = new JSONObject(params);
        PostJsonArrayRequest jsonArrayRequest = new PostJsonArrayRequest(Request.Method.POST,
                IpAddress.Service_Introduction, jsonObject,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray jsonArray) {
                        try {
                            JSONObject temp;
                            temp = (JSONObject) jsonArray.get(0);
                            serviceView.setText(temp.getString("content"));
                        } catch(Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e("ServiceIntroduceError",volleyError.getMessage(),volleyError);
                    }
                });
        requestQueue.add(jsonArrayRequest);
    }
}
